﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBC_Quiz_Show.Object;
using DevComponents.DotNetBar;
using KBC_Quiz_Show.Bussiness;
using System.Media;

namespace KBC_Quiz_Show.Presentation
{
    public partial class FrmGame : DevComponents.DotNetBar.Office2007RibbonForm
    {
        private Player p;
        private RadioButton[] arrRd;
        private LabelX[] arrLable;
        private QuesstionList qList;
        private string[] ansTrue;
        private Question[] q;
        private int count;
        QuestionBus qb = new QuestionBus();
       
        public FrmGame()
        {
            InitializeComponent();
        }

        private void FrmGame01_Load(object sender, EventArgs e)
        {

            SoundPlayer j = new SoundPlayer("C:\\Users\\LANHNGUYEN\\Desktop\\New folder\\DoAn_Final_01\\KBC Quiz Show\\KBC Quiz Show\\Music\\begin.wav");
            j.Play();
            qb.randomQues();
            
            arrLable = new LabelX[13];
            arrRd = new RadioButton[4];

            qList = qb.getQlist();
            ansTrue = qb.getAnstrue();
            int i = 0;
            arrLable[i] = label1;
            i++;
            arrLable[i] = label2;
            i++;
            arrLable[i] = label3;
            i++;
            arrLable[i] = label4;
            i++;
            arrLable[i] = label5;
            i++;
            arrLable[i] = label6;
            i++;
            arrLable[i] = label7;
            i++;
            arrLable[i] = label8;
            i++;
            arrLable[i] = label9;
            i++;
            arrLable[i] = label10;
            i++;
            arrLable[i] = label11;
            i++;
            arrLable[i] = label12;
            i++;
            arrLable[i] = label13;

            i = 0;
            arrRd[i] = rd1;
            i++;
            arrRd[i] = rd2;
            i++;
            arrRd[i] = rd3;
            i++;
            arrRd[i] = rd4;
            setQuestion();
        }

        public void changeBorder(LabelX a, LabelX b)
        {
            if (a != null)
            {
                a.BackColor = Color.Yellow;
            }
            b.BackColor = Color.Red;
        }

        public void resetRadio()
        {
            rd1.Enabled = true;
            rd2.Enabled = true;
            rd3.Enabled = true;
            rd4.Enabled = true;

            if (rd1.Checked) rd1.Checked = false;
            if (rd2.Checked) rd2.Checked = false;
            if (rd3.Checked) rd3.Checked = false;
            if (rd4.Checked) rd4.Checked = false;

        }

        public void setCount(int count)
        {
            this.count = count;
        }

        public void setQuestion()
        {
            if (count < qList.getN())
            {
                labelQues.Text = qList.getQues(count).QuesContent;
                rd1.Text = qList.getQues(count).Ans1;
                rd2.Text = qList.getQues(count).Ans2;
                rd3.Text = qList.getQues(count).Ans3;
                rd4.Text = qList.getQues(count).Ans4;
            }
        }

        public void setPlayer(Player p)
        {
            this.p = p;
            this.p = p;
            if (p != null)
            {
                lbName.Text = "Chào mừng bạn '" + p.PName + "' , '" + p.Age + "' tuổi, đến từ '" + p.PAddress + "' đã đến với chương trình AI LÀ TRIỆU PHÚ dành cho dân IT. Hãy trả lời các câu hỏi dưới đây và dành chiến thắng. GOOD LUCK!";
            }
        }

        int Prosess = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            labelQues.Text = "Xin Chờ trong giây lát, Máy Đang phân tích đáp án.";
            Prosess+=2;

            progressBarX1.Value = Prosess;
            if (Prosess == 100)
            {
                progressBarX1.Visible = false;
                Prosess = 0;
                labelQues.Text = qList.getQues(count).QuesContent;
                tmProcess.Stop();
                groupPanel2.BackColor = Color.Lime;
                tmDisplay.Start();
 
            }
        }   

        int display = 0;
        private void tmDisplay_Tick(object sender, EventArgs e)
        {           
            lbDisplay.Visible = true;
            lbDisplay.Enabled = true;
            rd1.Visible = false;
            rd2.Visible = false;
            rd3.Visible = false;
            rd4.Visible = false;
            display++;
            if (display == 30)
            {
                lbDisplay.Visible = false;
                lbDisplay.Enabled = false;
                rd1.Visible = true;
                rd2.Visible = true;
                rd3.Visible = true;
                rd4.Visible = true;
                tmDisplay.Stop();
                display = 0;
                //tang bien dem
                groupPanel2.BackColor = Color.Transparent;
                count++;
                setQuestion();
                resetRadio();
                //doi mau o
                if (count > 0 && count < qList.getN())
                {
                    changeBorder(arrLable[count - 1], arrLable[count]);
                }
            }
            
        }

        private void btnNext_Click_1(object sender, EventArgs e)
        {
            QuestionBus qBus = new QuestionBus();
            if (rd1.Checked == false && rd2.Checked == false && rd3.Checked == false && rd4.Checked == false)
            {
               // MessageBox.Show("You not select answer!");
                tmDisplay.Enabled = true;
                lbDisplay.Text = "You not select answer!";             
            }
            else
            {
                SoundPlayer j = new SoundPlayer("C:\\Users\\LANHNGUYEN\\Desktop\\New folder\\DoAn_Final_01\\KBC Quiz Show\\KBC Quiz Show\\Music\\true.wav");
                j.Play();
                 progressBarX1.Visible = true;
                 tmProcess.Enabled = true;

                if (count < qList.getN() && !qBus.checkAnswer(ansTrue, count, arrRd))
                {
                    //neu tra loi sai
                    int k = qList.getN();
                    int tien = 0;
                    int[] a;
                    a = new int[13];
                    a[0] = 5;
                    a[1] = 10;
                    a[2] = 20;
                    a[3] = 40;
                    a[4] = 80;
                    a[5] = 160;
                    a[6] = 320;
                    a[7] = 640;
                    a[8] = 1250;
                    a[9] = 2500;
                    a[10] = 5000;
                    a[11] = 10000;
                    a[12] = 50000;
                    int moc = 0;
                    if (count <= 2)
                    {
                        tien = 0;
                        moc = 0;
                    }
                    else if (count < 6 && count > 2)
                    {
                        moc = 1;
                    }
                    else if (count < 12)
                    {
                        tien = 3200;
                        moc = 6;
                    }
                    else
                    {
                        tien = a[count] * 10;
                    }
                    
                    PlayerBus pb = new PlayerBus();
                    p.Score = tien;
                    pb.insertPlayer(p);

                    lbDisplay.Text = "False answer! Your money earned is : '" + tien + "'$ \n True answer : '" + ansTrue[count] + "' ";
                    
                    changeBorder(arrLable[count], arrLable[moc]);
                    panelEx4.Visible = true;
                    lbStop.Text = "False answer! Your money earned is : '" + tien + "'$ \n True answer : '" + ansTrue[count] + "' ";
                    btnExit2.Visible = true;
                    //DialogResult result = MessageBox.Show("Are you sure quit???", "Confirm quit", MessageBoxButtons.YesNo);
                    //if (result == System.Windows.Forms.DialogResult.Yes)
                    //{
                    //    Form1 frm = new Form1();
                    //    frm.Show();
                    //    this.Hide();
                    //}
                }
                else
                {
                    
                   // tmDisplay.Enabled = true;
                    lbDisplay.Text = "Congratulations. You answered correctly";
                                       
                    
                    if (count == 13)
                    {
                        PlayerBus pb = new PlayerBus();
                        p.Score = 500000;
                        pb.insertPlayer(p);

                        lbDisplay.Text = "Congratulation!!!\n You are winner!!!\n Your money earned is : 500,000$ ";
                        panelEx4.Visible = true;
                        lbStop.Text = "Congratulation!!!\n You are winner!!!\n Your money earned is : 500,000$ ";
                        btnExit2.Visible = true;
                        rd1.Visible = false;
                        rd2.Visible = false;
                        rd3.Visible = false;
                        rd4.Visible = false;
                        labelQues.Text = "YOU ARE WINNER!!!";
                        btnStop.Visible = false;
                        btnNext.Visible = false;
                    }
                    
                }
            }
        }

        private void btnStop_Click_1(object sender, EventArgs e)
        {
            int[] a;
            a = new int[13];
            a[0] = 5;
            a[1] = 10;
            a[2] = 20;
            a[3] = 40;
            a[4] = 80;
            a[5] = 160;
            a[6] = 320;
            a[7] = 640;
            a[8] = 1250;
            a[9] = 2500;
            a[10] = 5000;
            a[11] = 10000;
            a[12] = 50000;

            DialogResult result = MessageBox.Show("Are you sure quit???", "Confirm quit", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                PlayerBus pb = new PlayerBus();
                if (count > 0) p.Score = a[count - 1] * 100;
                else p.Score = 0;
                pb.insertPlayer(p);
                int temp = a[count];
                if (count > 0)
                {
                    temp = a[count - 1];
                }
                else
                {
                    temp = 0;
                }
                // MessageBox.Show("Game Over\n Số tiền bạn kiếm được là : '" + temp * 10 + "'$");
                //this.Close();
                //tmDisplay.Enabled = true;                
               // lbDisplay.Text = "Game Over\n Số tiền bạn kiếm được là : '" + temp * 10 + "'$";
                panelEx4.Visible = true;
                lbStop.Text = "Game Over\n Số tiền bạn kiếm được là : '" + temp * 10 + "'$";
                btnExit2.Visible = true;
                rd1.Visible = false;
                rd2.Visible = false;
                rd3.Visible = false;
                rd4.Visible = false;
                btnNext.Visible = false;
                labelQues.Text = "STOP";
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btn5050_Click(object sender, EventArgs e)
        {
            QuestionBus qb = new QuestionBus();
            qb.choiceFifty(arrRd, ansTrue[count]);
            btn5050.Enabled = false;
        }

        private void btnFlip_Click(object sender, EventArgs e)
        {
            qb.choiceFlip(count, qList);          
            btnFlip.Enabled = false;
            this.resetRadio();
            this.setQuestion();
        }

        private void btnDouble_Click(object sender, EventArgs e)
        {
            QuestionBus qBus = new QuestionBus();
            if (!qBus.checkAnswer(ansTrue, count, arrRd))
            {
                //MessageBox.Show("dap an chua chinh xac. vui long chon lai :)");
                count--;
                tmDisplay.Enabled = true;
                lbDisplay.Text = "Answer is incorrect. Please choose again!";
            }
            else
            {
                count--;
                tmDisplay.Enabled = true;             
                lbDisplay.Text = "Congratulations. You answered correctly!";
            }
            btnDouble.Enabled = false;
        }

        private void btnExit2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }


}
